#include "msgnum.h"

int user_login(int fd,int status);			//用户登录函数
int query_self_msg(int fd);					//普通用户查询自己的用户信息
int change_self_msg(int fd);				//普通用户修改自己的用户信息
int query_user_msg(int fd);					//管理查询用户信息
int chang_user_msg(int fd);					//管理员修改用户信息
int del_user_msg(int fd);					//管理员删除用户
int add_user_msg(int fd);					//管理员添加用户
int list_user_msg(int fd);					//管理员查看所有用户的信息

int modify_pwd(int fd,struct MSG *msg);		//修改密码
int modify_sex(int fd,struct MSG *msg);		//修改性别
int modify_addr(int fd,struct MSG *msg);	//修改地址
int modify_phone(int fd,struct MSG *msg);	//修改电话
int modify_name(int fd,struct MSG *msg);	//修改姓名
int modify_money(int fd,struct MSG *msg);	//修改薪资


static char clean[64]={0}; 					

int main(int argc, const char *argv[])
{
	
	int sockfd;
	int cmd;
	struct sockaddr_in serveraddr;
	socklen_t len=sizeof(SA);
	struct MSG tell_server_quit;  //通知服务器,用户已退出的结构体
	if(argc!=3)
	{
		fprintf(stderr,"User:%s <IP> <port>\n",argv[0]);
		return -1;
	}

	if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
	{
		err_log("fail to socket");
	}
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr=inet_addr(argv[1]);

	if(connect(sockfd,(SA*)&serveraddr,len)<0)
	{
		err_log("fail to connect");
	}
/****************
 * 第一个用户界面
 * *************/
FIRST_INTERFACE:
	while(1)
	{
		puts("*********************Login***********************");
		puts("  1 .普通用户登录	 2 .管理员登录	3 .退出");
		puts("*************************************************");
		printf("Please input cmd >>>");
		fflush(stdout);
		if(scanf("%d",&cmd) != 1)
		{
			puts("input error");
			fgets(clean,64,stdin);
			continue;
		}

		switch(cmd)
		{
			case 1:
				if(user_login(sockfd,0) == OK)	//登录成功，跳转到普通用户界面
				{
					goto COM_LOG;
				}
				else
					break;
			case 2:
				if(user_login(sockfd,1) == OK)	//登录成功，跳转到管理员界面
				{
					goto ADM_LGO;
				}
				else
					break;
			case 3:								//退出员工管理系统
				exit(0);
			default:						
				puts("intput error");
				break;
		}
	}
/**************************************
 * 普通用户登录成功后，显示的可操作界面
 * ***********************************/
COM_LOG:
	while(1)
	{
		puts("*************************************************");
		puts("  1 .查询本人信息	 2 .修改本人信息	3 .退出");
		puts("*************************************************");
		printf("Please input cmd >>>");
		fflush(stdout);
		
		if(scanf("%d",&cmd) != 1)
		{
			puts("input error");
			fgets(clean,64,stdin);
			continue;
		}

		switch(cmd)
		{
			case 1: 
					query_self_msg(sockfd);
					break;
			case 2: 
					change_self_msg(sockfd);
					break;
			case 3: 
					tell_server_quit.cmd = BACK;
					send(sockfd,&tell_server_quit,sizeof(struct MSG),0);	//退出时通知服务器
					goto FIRST_INTERFACE;									//回跳到第一个界面
					break;
			default :
				puts("input error!");
				break;
		}
	}
	return 0;

/****************************************
 * 管理员用户登录成功后，显示的可操作界面
 * *************************************/
ADM_LGO:
	while(1)
	{
		puts("************************************************************************************");
		puts("  1.列出所有用户  2.查询用户信息  3.增加用户  4.删除用户  5.修改用户信息  6.退出");
		puts("************************************************************************************");
		printf("Please input cmd >>>");
		fflush(stdout);
		
		if(scanf("%d",&cmd) != 1)
		{
			puts("input error");
			fgets(clean,64,stdin);
			continue;
		}

		switch(cmd)
		{
			case 1:	list_user_msg(sockfd);
					break;
			case 2:	query_user_msg(sockfd);
					break;
			case 3:	add_user_msg(sockfd);
					break;
			case 4:	del_user_msg(sockfd);
					break;
			case 5:	chang_user_msg(sockfd);
					break;
			case 6:	tell_server_quit.cmd = BACK;					
					send(sockfd,&tell_server_quit,sizeof(struct MSG),0);	//通知服务器，客户端退出登录
					goto FIRST_INTERFACE;									//返回到第一个界面					
					break;
			default :
					puts("input error!");
					break;
		}

	}
	return 0;
}

int user_login(int fd,int status)
{
	struct login login_msg; 	//发送给服务器的登录信息结构体
	struct MSG	 login_return;	//接受服务器发送的返回结构体信息
	if(status == 0)
	{	//普通用户登录
		login_msg.flags = POWER_COM;
	}
	else{
		//管理员登录
		login_msg.flags = POWER_ADM;
	}
	puts("input number>>>");
	scanf("%s",login_msg.number);
	puts("input pwd>>>");
	scanf("%s",login_msg.password);
	/*发送登录信息*/
	send(fd,&login_msg,sizeof(struct login),0);
	/*接收返回信息*/
	recv(fd,&login_return,sizeof(struct MSG),0);

	if(login_return.cmd == OK)
	{
		puts("Login ok!");
		return OK;
	}
	else if(login_return.cmd == NO_USER)
	{	
		puts("NO THIS USER!");
		return NO_USER;
	}
	else 
	{
		puts("PWD error!");
		return ERROR;
	}
}

int query_self_msg(int fd)
{
	struct MSG my_msg;
	
	my_msg.cmd = COM_SHOW;
	
	send(fd,&my_msg,sizeof(struct MSG),0);
	
	recv(fd,&my_msg,sizeof(struct MSG),0);

	printf("%s\n",my_msg.msg);	//打印MSG结构提里的msg信息；
	
	return 0;
}

int change_self_msg(int fd)
{
	struct MSG my_msg;
	int cmd;
	puts("********************************************************************");
	puts("  1.修改密码 2.修改性别  3.修改地址  4.修改电话 5.修改姓名  6.退出");
	puts("********************************************************************");
	printf("Please input cmd >>>");
	fflush(stdout);

	if(scanf("%d",&cmd) != 1)
	{
		puts("input error");
		fgets(clean,64,stdin);
		return 0;
	}
	switch(cmd)
	{
		case 1:	modify_pwd(fd,&my_msg);
				break;
		case 2: modify_sex(fd,&my_msg);
				break;
		case 3:	modify_addr(fd,&my_msg);
				break;
		case 4:	modify_phone(fd,&my_msg);
				break;
		case 5:	modify_name(fd,&my_msg);
				break;
		case 6: return 0;
		default :
				printf("input error!");
				break;
	}
	return 0 ;
}

int query_user_msg(int fd)
{
	struct MSG query_msg;
	query_msg.cmd = ADM_SHOW;
	puts("input user number>>");
	scanf("%s",query_msg.msg);
	send(fd,&query_msg,sizeof(struct MSG),0);
	recv(fd,&query_msg,sizeof(struct MSG),0);
	if(query_msg.cmd == OK)
	{
		printf("%s\n",query_msg.msg);
	}
	else if(query_msg.cmd == NO_USER)
	{
		printf("NO THIS USER!\n");
	}
	return 0;
}
int chang_user_msg(int fd)
{
	int cmd;
	struct MSG my_msg;
	my_msg.cmd = ADM_RVS;
	puts("input change user number>>");
	scanf("%s",my_msg.msg);
	send(fd,&my_msg,sizeof(struct MSG),0);
	recv(fd,&my_msg,sizeof(struct MSG),0);
	if(my_msg.cmd == OK)
	{

		puts("*******************************************************************************");
		puts("  1.修改密码 2.修改性别  3.修改地址  4.修改电话 5.修改姓名  6.修改薪资 7.退出");
		puts("*******************************************************************************");
		printf("Please input cmd >>>");
		fflush(stdout);

		if(scanf("%d",&cmd) != 1)
		{
			puts("input error");
			fgets(clean,64,stdin);
			return 0;
		}

		switch(cmd)
		{
			case 1:	modify_pwd(fd,&my_msg);
					break;
			case 2: modify_sex(fd,&my_msg);
					break;
			case 3:	modify_addr(fd,&my_msg);
					break;
			case 4:	modify_phone(fd,&my_msg);
					break;
			case 5:	modify_name(fd,&my_msg);
					break;
			case 6: modify_money(fd,&my_msg);
					break;
			case 7: return 0;
			default :
					printf("input error!");
					break;
		}
	}
	else if(my_msg.cmd == NO_USER)
	{
		printf("NO THIS USER\n");
	}

	return 0;
}
int del_user_msg(int fd)
{
	struct MSG del_msg;
	del_msg.cmd = ADM_DEL;
	puts("input user number>>");
	scanf("%s",del_msg.msg);
	send(fd,&del_msg,sizeof(struct MSG),0);
	recv(fd,&del_msg,sizeof(struct MSG),0);
	if(del_msg.cmd == OK)
	{
		printf("delete successful!\n");
	}
	else if(del_msg.cmd == NO_USER)
	{
		printf("NO THIS USER!\n");
	}

	return 0;
}
int add_user_msg(int fd)
{
	struct ADD_USER add_user;
	struct MSG msg;
	msg.cmd = ADM_ADD;
	puts("input user number>>");
	scanf("%s",msg.msg);
	send(fd,&msg,sizeof(struct MSG),0);
	recv(fd,&msg,sizeof(struct MSG),0);
	if(msg.cmd == OK)
	{
		puts("input user limit>>");
		scanf("%d",&(add_user.power));
		puts("input user name>>");
		scanf("%s",add_user.name);
		puts("input user pwd>>");
		scanf("%s",add_user.password);
		puts("input user sex>>");
		scanf("%d",&(add_user.sex));
		puts("input addr>>");
		scanf("%s",add_user.addr);
		puts("input phone >>");
		scanf("%s",add_user.phone);
		puts("input money>>");
		scanf("%ld",&(add_user.money));
		send(fd,&add_user,sizeof(struct ADD_USER),0);
		recv(fd,&msg,sizeof(struct MSG),0);
		if(msg.cmd == OK)
		{
			printf("add user successful\n");
		}
		else{
			printf("add failed\n");
		}
	}
	else if(msg.cmd == REPEAT )
	{
		printf("user exited\n");
	}
	
	return 0;
}
int list_user_msg(int fd)
{
	struct MSG list_msg;
	list_msg.cmd = ADM_LIST;
	send(fd,&list_msg,sizeof(struct MSG),0);
	recv(fd,&list_msg,sizeof(struct MSG),0);
	while(list_msg.cmd != OK)
	{
		printf("%s\n",list_msg.msg);
		recv(fd,&list_msg,sizeof(struct MSG),0);
	}
	printf("%s\n",list_msg.msg);
	return 0;
}
/**********************************************************************
* 以下的函数可以用一个函数来代替，在调用的时候多传一个status的参数就行
* int modify(int fd,struct MSG *msg,int status)
* {
* 	switch(status)
* 	{
*		case RVS_PWD: msg->cmd = status;
*					  puts("input new password>>");
*					  scanf("%s",msg->msg);
*					  break;
*		case RVS_SEX: ...;
*		...
*		default :	 break;
* 	}
*	send();
*	recv();
*	if(...)
*	{
*		...
*	}
*	else
*	{
*		...
*	}
*	return 0;
* }
 * ********************************************************************/
int modify_pwd(int fd,struct MSG *msg)
{
	msg->cmd = RVS_PWD;
	puts("input new password>>");
	scanf("%s",msg->msg);
	send(fd,msg,sizeof(struct MSG),0);
	recv(fd,msg,sizeof(struct MSG),0);
	if(msg->cmd == OK)
	{
		printf("modify successful!\n");
	}
	else 
	{
		printf("modify failed,please try again!\n");
	}
	return 0;
}
int modify_sex(int fd,struct MSG *msg)
{
	msg->cmd = RVS_SEX;
	puts("input now sex>>");
	scanf("%s",msg->msg);
	send(fd,msg,sizeof(struct MSG),0);
	recv(fd,msg,sizeof(struct MSG),0);
	if(msg->cmd == OK)
	{
		printf("modify successful!\n");
	}
	else 
	{
		printf("modify failed,please try again!\n");
	}
	return 0;
}
int modify_addr(int fd,struct MSG *msg)
{
	msg->cmd = RVS_ADDR;
	puts("input new addr>>");
	scanf("%s",msg->msg);
	send(fd,msg,sizeof(struct MSG),0);
	recv(fd,msg,sizeof(struct MSG),0);
	if(msg->cmd == OK)
	{
		printf("modify successful!\n");
	}
	else 
	{
		printf("modify failed,please try again!\n");
	}
	return 0;
}
int modify_phone(int fd,struct MSG *msg)
{
	msg->cmd = RVS_PHONE;
	puts("input new phone number>>");
	scanf("%s",msg->msg);
	send(fd,msg,sizeof(struct MSG),0);
	recv(fd,msg,sizeof(struct MSG),0);
	if(msg->cmd == OK)
	{
		printf("modify successful!\n");
	}
	else 
	{
		printf("modify failed,please try again!\n");
	}
	return 0;
}
int modify_name(int fd,struct MSG *msg)
{
	msg->cmd = RVS_NAME;
	puts("input new name>>");
	scanf("%s",msg->msg);
	send(fd,msg,sizeof(struct MSG),0);
	recv(fd,msg,sizeof(struct MSG),0);
	if(msg->cmd == OK)
	{
		printf("modify successful!\n");
	}
	else 
	{
		printf("modify failed,please try again!\n");
	}
	return 0;
}

int modify_money(int fd,struct MSG *msg)
{
	msg->cmd = RVS_MONEY;
	puts("input new money>>");
	scanf("%s",msg->msg);
	send(fd,msg,sizeof(struct MSG),0);
	recv(fd,msg,sizeof(struct MSG),0);
	if(msg->cmd == OK)
	{
		printf("modify successful!\n");
	}
	else 
	{
		printf("modify failed,please try again!\n");
	}
	return 0;	
}
